package com.webapp.controller;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.webapp.model.Application;
import com.webapp.model.Review;
import com.webapp.model.ReviewModel;
import com.webapp.model.User;
import com.webapp.model.ViewApplicationModel;

/**
 * Servlet implementation class ViewApplication
 */
public class ViewApplication extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ViewApplication() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		/**
		 * get the details of the email 
		 */
		
		//get the session
		HttpSession session = request.getSession();
		
		//get value from submitted form
		String email = request.getParameter("appID");
		
		//call method with app id param
		System.out.println("application email " + email);
		
		// return the application view
		ViewApplicationModel viewApp = new ViewApplicationModel();
		Application app = viewApp.getApplication(email);
		session.setAttribute("Application", app);
		
		// get the user 
		User user = (User)session.getAttribute("UserObject"); 

		// get the review
		ReviewModel rm = new ReviewModel();
		Review review = rm.getExistingReview(email, user.getEmail());
		System.out.println("review" + review);
		session.setAttribute("Review", review);
		
		RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/viewSingleAppsTest.jsp");								
		dispatcher.forward(request, response);
	}
}